<?php
class LoanSettlementReviewNotification extends CWidget {

	public $dayFrom=-150;
	public $dayTo=-90;
	
	public $column='settlementDate';
	public $title='Loans to be reviewed - Settled 3-5 months ago';

	public function run()
	{
		$column=$this->column;
		$dateFormat='Y-m-d';
		$criteria=new CDbCriteria();
		$currentTime=mktime (0, 0, 0 , date("n") ,date("j"),  date("Y") );

		$criteria->addBetweenCondition($column, date($dateFormat, $currentTime+86400*$this->dayFrom),
		date($dateFormat, $currentTime+86400*$this->dayTo)
		);
		$criteria->order=$this->column.' asc';

		if(!Yii::app()->user->checkAccess('customerInfoView')){
			if(User::getCurrentUser()->teamLeader){
				$criteria->compare('teamId', User::getCurrentUser()->teamId);
			}else{
				$criteria->compare('createUserId', User::getCurrentUser()->id);				
			}
		}
		$criteria->compare('settlementReviewed', '0');
		$criteria->with=array('customer');
		$loansDb=Loan::model()->findAll($criteria);
		$loans=array();
		foreach ($loansDb as $loanDb){
			$date=CDateTimeParser::parse($loanDb->$column,'d/M/yyyy');
			$loans[]=array(
				'id'=>$loanDb->id,
				'customer'=>$loanDb->customer,
				'bank'=>$loanDb->bank,
				'date'=>$loanDb->$column,
				'daysLeft'=>intval(($currentTime-$date)/86400),
			);
		}
		$this->render('loanSettlementReviewNotification',array('loans'=>$loans/*,'dayOffset'=>$this->dayOffset*/));
	}
}